module nade.think;

import std.stdio;

import nade.time;


class Thought {
	this(double t, double delegate(double) d)
	{
		time = t;
		dg = d;
	}

	double time;
	double delegate(double) dg;
	Thought next = null;
}


private {
	Thought _thoughts = null;
}


void think(double time)
{
	while(_thoughts !is null  &&  _thoughts.time <= time)
	{
		auto t = _thoughts;
		assert(t.dg !is null);

		_thoughts = _thoughts.next;
		double r = t.time;
		while(r <= time  &&  r > 0)
		{
			r = t.dg(r);
		}

		if(r > time)
			addThought(r, t.dg);
	}
}


void addThought(double time, double delegate(double) dg)
{
	auto t = new Thought(time, dg);
	Thought* p = &_thoughts;
	while(*p !is null)
	{
		if((*p).time > time)
		{
			t.next = *p;
			*p = t;
			return;
		}

		p = &(*p).next;
	}

	t.next = null;
	*p = t;
}
